package com.android.example.app3;

import android.util.Log;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedList;

public class RulesFileUpdater {
    private static final String TAG = "RulesFileUpdater";
    private LinkedList<String> photoList;
    private LinkedList<String> danhaoList;

    public RulesFileUpdater() {
        photoList = new LinkedList<>();
        danhaoList = new LinkedList<>();
    }

    public void updateRulesFile(String fileName) {
        // 获取应用专属外部存储目录下的规则文件
        File rulesFile = new File(getExternalFilesDir(null), fileName);
        // 检查规则文件是否存在
        if (rulesFile.exists()) {
            try (BufferedReader reader = new BufferedReader(new FileReader(rulesFile))) {
                String line;
                if (fileName.equals("photoList.txt")) {
                    // 清空 photoList
                    photoList.clear();
                    // 逐行读取规则文件
                    while ((line = reader.readLine()) != null) {
                        photoList.add(line);
                    }
                    // 删除多余元素
                    while (photoList.size() > 2000) {
                        photoList.remove(2000); // 移除最旧的数据
                    }
                } else if (fileName.equals("danhaoList.txt")) {
                    // 清空 danhaoList
                    danhaoList.clear();
                    // 逐行读取规则文件
                    while ((line = reader.readLine()) != null) {
                        danhaoList.add(0, line);
                    }
                }
            } catch (IOException e) {
                // 日志记录错误，实际应用中需要记录日志
                Log.e(TAG, "updateRulesFile: ", e);
            }
        }
    }

    public LinkedList<String> getPhotoList() {
        return photoList;
    }

    public LinkedList<String> getDanhaoList() {
        return danhaoList;
    }

    // 示例方法，用于获取外部存储目录
    private File getExternalFilesDir(String type) {
        // 这里需要根据实际情况实现获取外部存储目录的逻辑
        // 例如，在Activity或Service中可以使用 getExternalFilesDir(null)
        return null;
    }
}
